package longrise.phone.com.bjjt_henan.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.bjjt.util.UiUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * Created by Administrator on 2017-6-27.
 */

public class AccidentTypeAdapter extends BaseAdapter {

    private Context mContext;

    private List<EntityBean> childLists;

    private Dialog mDialog;

    public AccidentTypeAdapter(Context context) {
        this.mContext = context;
    }

    public void setChildLists(List<EntityBean> childLists) {
        this.childLists = childLists;
    }

    @Override
    public int getCount() {
        return (childLists == null) ? 0 : childLists.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildVHolder holder = null;
        if (convertView == null) {
            holder = new ChildVHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_accidenttype_item, null);
            holder.infoTV = (CheckedTextView) convertView.findViewById(R.id.selecttype_child_ctv);
            holder.iconIV = (ImageView) convertView.findViewById(R.id.selecttype_child_iv);
            convertView.setTag(holder);
        } else {
            holder = (ChildVHolder) convertView.getTag();
        }
        EntityBean data = childLists.get(position);
        holder.infoTV.setText(data.getString("desc"));
        boolean check = ((ListView) parent).isItemChecked(position);
        holder.infoTV.setChecked(check);
        final String icon = data.getString("icon");
        ImageLoader.getInstance().displayImage("drawable://" + icon, holder.iconIV, CommontUtils.getOptions());
        holder.iconIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAccidentDialog(icon);
            }
        });

        return convertView;
    }

    private ImageView selectIV;

    private void showAccidentDialog(String icon) {
        if (mDialog == null) {
            mDialog = new Dialog(mContext, R.style.AlertDialogStyle);
            View view = View.inflate(mContext, R.layout.dialog_accident_detail, null);
            selectIV = (ImageView) view.findViewById(R.id.dialog_iv_accident);
            view.findViewById(R.id.dialog_iv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
            mDialog.setContentView(view);

            Window dialogWindow = mDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.90);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) selectIV.getLayoutParams();
            int rWidth = (int) (lp.width - UiUtil.dip2Px(15 * 2));
            params.width = rWidth;
            params.height = rWidth * 25 / 36;
            selectIV.setLayoutParams(params);

            dialogWindow.setAttributes(lp);
        }
        ImageLoader.getInstance().displayImage("drawable://" + icon, selectIV, CommontUtils.getOptions());
        mDialog.show();
    }

    private class ChildVHolder {
        private CheckedTextView infoTV;

        private ImageView iconIV;
    }
}
